package com.qcby.service.impl;

import com.qcby.dao.UserMapper;
import com.qcby.model.User;
import com.qcby.model.dto.UserQueryDto;
import com.qcby.query.UserQuery;
import com.qcby.service.UserService;
import com.qcby.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends BaseServiceImpl<UserQuery, User> implements
        UserService {


    private UserMapper userDao;

    @Autowired
    public void setBrandDao(UserMapper userDao) {
        this.userDao = userDao;
        this.baseDao = userDao;
    }

    @Override
    public User login(User user) {
        return userDao.login(user);
    }

    @Override
    public Integer isSameName(User user) {
        return userDao.isSameName(user);
    }

    @Override
    public List<User> managerLogin(User user) {
        return userDao.managerLogin(user);
    }

    @Override
    public Page<User> query(UserQueryDto userQueryDto) {
        Integer pageNo = userQueryDto.getPageNo();
        Integer pageSize = userQueryDto.getPageSize();
        Integer startNum = (pageNo - 1) * pageSize;

        Page<User> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setStartNum(startNum);

        String orderBy;
        List<User> showVoList = userDao.query(userQueryDto.getUsername(), userQueryDto.getDisplayName(), userQueryDto.getPhone(), startNum, pageSize);
        Integer count = userDao.queryCount(userQueryDto.getUsername(), userQueryDto.getDisplayName(), userQueryDto.getPhone());

        page.setTotalCount(count);
        page.setList(showVoList);
        page.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1);
        return page;
    }
}
